<?php
namespace app\Worker\controller;
use think\worker\Server;

class Inlet extends Server{
    protected $socket = 'websocket://192.168.80.128:2346';
    /**
     * 收到信息
     * @param $connection
     * @param $data
     */

    protected $client = array();

    public function _initialize(){
        $this->worker->count = 1;
    }

    public function onMessage($connection, $data){
        $data=$this->turnJson($data);
        if(!$data){
            $connection->send($this->error("Json"));
            exit;
        }
        if(!isset($data['model'])){
            $connection->send($this->error("Parameter"));
            exit;
        }
        switch($data['model']){
            case "setName":
                if(!isset($data['name'])){
                    $connection->send($this->error("Name"));
                    exit;
                }
                if(!trim($data['name'])){
                    $connection->send($this->error("Name"));
                    exit;
                }
                $this->client[$connection->id] = array("connection"=>$connection, "name"=>$data['name']);
                foreach($this->client as $v){
                    $v['connection']->send($this->success(2,"success",$v['connection']->id,$v['connection']->id,$this->backPeople($connection)));
                }
            break;
            case "msg":
                if(!isset($data['id'])){
                    $connection->send($this->error("ID"));
                    exit;
                }
                if(!intval($data['id'])){
                    $connection->send($this->error("ID"));
                    exit;
                }
                $this->client[$connection->id]['connection']->send($this->success(1,$data['msg'],$connection->id,$connection->id));
                $this->client[$data['id']]['connection']->send($this->success(1,$data['msg'],$connection->id,$data['id']));
            break;
        }
    }

    public function backPeople($connection){
        $people=array();
        foreach($this->client as $k=>$v){
            $people[]=array("id"=>$k,"name"=>$v['name']);
        }
        return $people;
    }

    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection){
        if(empty($this->client[$connection->id])) $connection->send($this->error("Nickname"));  
    }

    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection){
        unset($this->client[$connection->id]);
    }

    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg){
        echo "error $code $msg\n";
    }

    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker){

    }

    // json 转换 数组
    public function turnJson($data){
        $data = json_decode($data,true);
        if ($data && (is_object($data)) || (is_array($data) && !empty(current($data)))) {
            return $data;
        }
        return false;
    }

    public function success($code,$msg,$send_id,$collect_id,$data=array()){
        return json_encode(array("code"=>$code,"msg"=>$msg,"send_id"=>$send_id,"send_name"=>$this->client[$send_id]['name'],"collect_id"=>$collect_id,"data"=>$data));
    }

    // 返回固定信息
    public function error($model){
        switch($model){
            case "Nickname":
                return json_encode(array("code"=>-1,"msg"=>"连接成功请输入昵称"));
            break;
            case "Json":
                return json_encode(array("code"=>0,"msg"=>"数据格式错误"));
            break;
            case "Name":
                return json_encode(array("code"=>0,"msg"=>"请输入用户昵称"));
            break;
            case "ID":
                return json_encode(array("code"=>0,"msg"=>"请选择要发送的人"));
            break;
            case "Parameter":
                return json_encode(array("code"=>0,"msg"=>"系统繁忙"));
            break;
        }
    }
}
